package cn.com.xyc.Controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

import org.junit.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import cn.com.xyc.pojo.User;
import cn.com.xyc.rabbitmq.Producer;
import cn.com.xyc.rabbitmq.QueueConsumer;

@ContextConfiguration(locations = { "classpath:spring/spring-rabbit.xml" })
public class MQController extends BaseController {
	@Autowired
	private AmqpTemplate logAmqpTemplate;

	@Test
	public void test1() {
		try {
			QueueConsumer consumer;
			consumer = new QueueConsumer("queue1");
			Thread consumerThread = new Thread(consumer);
			consumerThread.start();

			Producer producer = new Producer("queue1");

			for (int i = 0; i < 1000000; i++) {
				HashMap message = new HashMap();
				message.put("message number", i);
				producer.sendMessage(message);
				System.out.println("Message Number " + i + " sent.");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test2() {
		User admin = new User();
		admin.setId("2");
		admin.setActivation("43");
		admin.setCreateDate(new Date());
		admin.setUsername("张三");
		admin.setPassword("1234567890");
		logAmqpTemplate.convertAndSend("queue_log_1_key",admin);

	}
}
